﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.Common;

namespace WarehouseManager.View
{
    public partial class PatientSelectForm : Form
    {
        private string stroperator;
        private DataTable datSource;
        private bool bSetInfpFlg = false;

        public SetInfoCallBack SelectPatientInfo { get; set; }

        public PatientSelectForm(string _operator,DataTable DatInfo)
        {
            InitializeComponent();

            this.stroperator = _operator;
            this.Init(DatInfo);
        }

        private void Init(DataTable DatInfo)
        {
            this.lblOperator.Text = this.stroperator;
            this.lblOpDate.Text = DateTimeUtil.GetCurrentDate();

            this.datSource = this.CreateDataTable();
            this.datGridList.DataSource = this.datSource;

            this.MergeData(DatInfo);

        }

        private DataTable CreateDataTable()
        {
            DataTable datTemp = new DataTable();
            //ID
            datTemp.Columns.Add(ConstInfo.COL_PATIENT_ID, typeof(string));
            //编号
            datTemp.Columns.Add(ConstInfo.COL_NUMBER, typeof(string));
            //姓名
            datTemp.Columns.Add(ConstInfo.COL_PATIENT_NAME, typeof(string));
            //拼音码
            datTemp.Columns.Add(ConstInfo.COL_PYCODE, typeof(string));
            //性别
            datTemp.Columns.Add(ConstInfo.COL_SEX, typeof(string));
            //联系电话
            datTemp.Columns.Add(ConstInfo.COL_MOBILENO, typeof(string));
            //身份证号
            datTemp.Columns.Add(ConstInfo.COL_PIDNUMBER, typeof(string));
            //出生日期
            datTemp.Columns.Add(ConstInfo.COL_BIRTHDAY, typeof(string));
            //家庭住址
            datTemp.Columns.Add(ConstInfo.COL_ADDR, typeof(string));          
            //入院日期
            datTemp.Columns.Add(ConstInfo.COL_RYDATE, typeof(string));
            //出院日期
            datTemp.Columns.Add(ConstInfo.COL_CYDATE, typeof(string));
            //疾病名称
            datTemp.Columns.Add(ConstInfo.COL_SICKNESSNAME, typeof(string));
            //工作单位
            datTemp.Columns.Add(ConstInfo.COL_WORKPLACE, typeof(string));
            //备注
            datTemp.Columns.Add(ConstInfo.COL_NOTE, typeof(string));
            return datTemp;
        }

        private void MergeData(DataTable datTemp)
        {
            foreach (DataRow dar in datTemp.Rows)
            {
                DataRow darSource = this.datSource.NewRow();
                //ID
                darSource[ConstInfo.COL_PATIENT_ID] = dar[ConstInfo.COL_PATIENT_ID];
                //编号
                darSource[ConstInfo.COL_NUMBER] = dar[ConstInfo.COL_NUMBER];
                //姓名
                darSource[ConstInfo.COL_PATIENT_NAME] = dar[ConstInfo.COL_PATIENT_NAME];
                //拼音码
                darSource[ConstInfo.COL_PYCODE] = dar[ConstInfo.COL_PYCODE];
                //性别
                darSource[ConstInfo.COL_SEX] = dar[ConstInfo.COL_SEX].ToString() == "1" ? "女" : "男";
                //联系电话
                darSource[ConstInfo.COL_MOBILENO] = dar[ConstInfo.COL_MOBILENO];
                //身份证号
                darSource[ConstInfo.COL_PIDNUMBER] = dar[ConstInfo.COL_PIDNUMBER];
                //出生日期
                darSource[ConstInfo.COL_BIRTHDAY] = dar[ConstInfo.COL_BIRTHDAY];
                //家庭住址
                darSource[ConstInfo.COL_ADDR] = dar[ConstInfo.COL_ADDR];
                //入院日期
                darSource[ConstInfo.COL_RYDATE] = dar[ConstInfo.COL_RYDATE];
                //出院日期
                darSource[ConstInfo.COL_CYDATE] = dar[ConstInfo.COL_CYDATE];
                //疾病名称
                darSource[ConstInfo.COL_SICKNESSNAME] = dar[ConstInfo.COL_SICKNESSNAME];
                //工作单位
                darSource[ConstInfo.COL_WORKPLACE] = dar[ConstInfo.COL_WORKPLACE];
                //备注
                darSource[ConstInfo.COL_NOTE] = dar[ConstInfo.COL_NOTE];
                datSource.Rows.Add(darSource);
            }
            this.datSource.AcceptChanges();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.datGridList.SelectedRows.Count != 1)
            {
                this.errorProvider.SetError(this.btnSelect, "请选中一行再操作");
                return;
            }

            DataRow rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];

            if (SelectPatientInfo != null)
            {
                SelectPatientInfo(rowSelect);
            }
            bSetInfpFlg = true;
            this.Close();
        }

        private void datGridList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataRow rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];

            if (SelectPatientInfo != null)
            {
                SelectPatientInfo(rowSelect);
            }
            bSetInfpFlg = true;
            this.Close();
        }

        private void PatientSelectForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bSetInfpFlg)
            {
                return;
            }

            DataRow rowSelect = null;
            if (this.datGridList.SelectedRows.Count != 1)
            {
                rowSelect = this.datSource.Rows[0];
            }
            else
            {
                rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];
            }

            if (this.SelectPatientInfo != null)
            {
                SelectPatientInfo(rowSelect);
            }
        }

    }
}
